<?php
include('includes/connexion.php');
$adminmanager = new AdminManager($connexion);
$elevemanager = new EleveManager($connexion);
session_start();
include('includes/secu.php');

$collegemanager = new CollegeManager($connexion);

if(isset($_POST['vformcollege'])){
	$newcollege = new College();
	$newcollege->setNom($_POST['nom']);
	$newcollege->setAdresse($_POST['adresse']);
	$newcollege->setVille($_POST['ville']);
	$newcollege->setCp($_POST['codepostal']);
	$collegemanager->add($newcollege);
}

if(isset($_POST['mformcollege'])){
	$mcollege = new College();
	$mcollege->setId($_POST['id']);
	$mcollege->setNom($_POST['nom']);
	$mcollege->setAdresse($_POST['adresse']);
	$mcollege->setVille($_POST['ville']);
	$mcollege->setCp($_POST['cp']);
	$collegemanager->update($mcollege);
}

if(isset($_GET['del']))
		$collegemanager->delete($collegemanager->get($_GET['id']));	

?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <title>Tables Advanced - Target Admin</title>

  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">

  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300,700">
  <link rel="stylesheet" href="./css/font-awesome.min.css">
  <link rel="stylesheet" href="./js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.min.css">
  <link rel="stylesheet" href="./css/bootstrap.min.css">

  <!-- Plugin CSS -->
  <link rel="stylesheet" href="./js/plugins/icheck/skins/minimal/blue.css">
  <link rel="stylesheet" href="js/plugins/select2/select2.css">
  <link rel="stylesheet" href="js/plugins/datepicker/datepicker.css">
  <link rel="stylesheet" href="js/plugins/simplecolorpicker/jquery.simplecolorpicker.css">
  <link rel="stylesheet" href="js/plugins/timepicker/bootstrap-timepicker.css">
  <link rel="stylesheet" href="js/plugins/fileupload/bootstrap-fileupload.css">
  
  <!-- App CSS -->
  <link rel="stylesheet" href="./css/target-admin.css">
  <link rel="stylesheet" href="./css/custom.css">


  <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
  <![endif]-->
</head>

<body>

<?php include('includes/navbar.php'); ?>

<?php include('includes/mainbar.php'); ?>

<div class="container">

  <div class="content">

    <div class="content-container">

      <div class="content-header">
        <h2 class="content-header-title">Liste des collèges</h2>
      </div> <!-- /.content-header -->

      <div class="row">

        <div class="col-md-12">

          <div class="portlet">

            <div class="portlet-header">

              <h3>
                <i class="fa fa-table"></i>
                Liste des collèges
              </h3>
             

            </div> <!-- /.portlet-header -->

            <div class="portlet-content">           

              <div class="table-responsive">

              <table 
                class="table table-striped table-bordered table-hover table-highlight table-checkable" 
                data-provide="datatable" 
                data-display-rows="10"
                data-info="true"
                data-search="true"
                data-length-change="true"
                data-paginate="false"
              >
                  <thead>
                    <tr>
                      <th class="col-lg-3"></th>
                      <th data-filterable="true" data-sortable="true" data-direction="desc">Nom</th>
                      <th data-filterable="true">Adresse</th>
                      <th data-filterable="true" data-sortable="true">Ville</th>
                      <th data-filterable="true" data-sortable="true">Code Postal</th>
                    </tr>
                  </thead>
                  <tbody>
<?php foreach($collegemanager->getList() as $key => $value){ ?>
                    <tr id="college<?php echo $value->id(); ?>">
                      <td class="checkbox-column"><a data-toggle="modal" href="#ModifCollege" class="fa fa-gear"></a> | <a href="college.php?id=<?php echo $value->id(); ?>&del" style='color:red;' onclick="return confirm('Etes-vous sur de vouloir supprimer ce collège?');"><span class="fa fa-trash-o"></span></a> | <a href="classe.php?id=<?php echo $value->id(); ?>">Gestion des classes</a></td>
                      <td><?php echo $value->nom();?></td>
                      <td><?php echo $value->adresse();?></td>
                      <td><?php echo $value->ville();?></td>
                      <td><?php echo $value->cp();?></td>
                    </tr>
<?php } ?>
                  </tbody>
                </table>
              </div> <!-- /.table-responsive -->
              <form action="college.php" id="formcollege" method="post" class="form parsley-form">
              <div class="col-md-3">
              <label for="nom">Nom</label><input type="text" name="nom" class="form-control"/>
              </div>
              <div class="col-md-4">
              <label for="adresse">Adresse</label><input type="text" name="adresse" class="form-control"/>
              </div>
              <div class="col-md-2">
              <label for="ville">Ville</label><input type="text" name="ville" class="form-control"/>
              </div><div class="col-md-2">
              <label for="codepostal">Code Postal</label><input type="text" name="codepostal" class="form-control"/>
              </div><div class="col-md-1">
              <br><input type="submit" value="Valider" name="vformcollege" class="btn btn-default"/>
              </div>
                  </form>              

            </div> <!-- /.portlet-content -->

          </div> <!-- /.portlet -->        

        </div> <!-- /.col -->

      </div> <!-- /.row -->

    </div> <!-- /.content-container -->
      
  </div> <!-- /.content -->

</div> <!-- /.container -->


<?php include('includes/footer.php'); ?>

  <script src="./js/libs/jquery-1.10.1.min.js"></script>
  <script src="./js/libs/jquery-ui-1.9.2.custom.min.js"></script>
  <script src="./js/libs/bootstrap.min.js"></script>

  <!--[if lt IE 9]>
  <script src="./js/libs/excanvas.compiled.js"></script>
  <![endif]-->
  
  <!-- Plugin JS -->
  <script src="./js/plugins/datatables/jquery.dataTables.min.js"></script>
  <script src="./js/plugins/datatables/DT_bootstrap.js"></script>
  <script src="./js/plugins/tableCheckable/jquery.tableCheckable.js"></script>
  <script src="./js/plugins/icheck/jquery.icheck.min.js"></script>

  <!-- App JS -->
  <script src="./js/target-admin.js"></script>

<div id="ModifCollege" class="modal modal-styled fade">
  <div class="modal-dialog">
    <div class="modal-content">
    <form action="college.php" id="mformcollege" method="post" class="form parsley-form">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3 class="modal-title">Modification de l'accès</h3>
      </div>
      <div class="modal-body">
      	<div class="row">
              <div class="col-md-6">
              <label for="nom">Nom de l'établissement</label><input type="text" name="nom"/>
              </div>
              <div class="col-md-6">
              <label for="adresse">Adresse postale</label><input type="text" name="adresse"/>
              </div>
      	</div>
      	<div class="row">
              <div class="col-md-6">
              <label for="ville">Ville de l'établissement</label><input type="text" name="ville"/>
              </div>
              <div class="col-md-5">
              <label for="cp">Code Postal</label><input type="text" name="cp"/>
              </div>
      	</div>
      </div>
      <div class="modal-footer">
      	<input type="text" name="id" hidden/>
        <button type="button" class="btn btn-tertiary" data-dismiss="modal">Fermer</button>
        <input type="submit" class="btn btn-primary" name="mformcollege" value="Sauvegarder"/>
      </div>
      </form>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script type="text/javascript">
$(document).ready(function() {
	$("[id^='college'] a.fa").click(function() {

		var id = $(this).parent().parent().attr("id").split("college");
		$("#mformcollege input[name=id]").val(id[1]);
		
		var td2 = "[id^='college" + id[1] + "'] td+td";
		var td3 = "[id^='college" + id[1] + "'] td+td+td";
		var td4 = "[id^='college" + id[1] + "'] td+td+td+td";
		var td5 = "[id^='college" + id[1] + "'] td+td+td+td+td";
		
		$("#mformcollege input[name=nom]").val($(td2).html());
		
		$("#mformcollege input[name=adresse]").val($(td3).html());
		
		$("#mformcollege input[name=ville]").val($(td4).html());
		
		$("#mformcollege input[name=cp]").val($(td5).html());
	});
});
</script>
  
</body>
</html>